package cn.shop.dys.adapter;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.shop.dys.R;
import cn.shop.dys.bean.PayCenterItem;
import cn.shop.dys.util.PayCenterUtils;
import cn.shop.dys.widget.ExpandableLayout;
import cn.shop.dys.widget.ExpandableLayoutListenerAdapter;
import cn.shop.dys.widget.ExpandableLinearLayout;
import cn.shop.lib_common.InterpolatorUtils;

public class PayCenterAdapter extends RecyclerView.Adapter<PayCenterAdapter.ViewHolder> {

    private final List<PayCenterItem> data;
    private Context context;
    private SparseBooleanArray expandState = new SparseBooleanArray();

    public PayCenterAdapter(final List<PayCenterItem> data) {
        this.data = data;
        for (int i = 0; i < data.size(); i++) {
            expandState.append(i, false);
        }
    }

    @Override
    public int getItemViewType(int position) {
        int type = PayCenterUtils.getType(data.get(position).key);
        if (type >= 0) {
            return type;
        }
        return PayCenterUtils.getType(data.get(position).key);
    }


    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
        this.context = parent.getContext();
        ViewHolder viewHolder = null;
        switch (viewType) {
            case 0:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_addressinfo, parent, false));
                break;
            case 1:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_paymentlist, parent, false));
                break;
            case 2:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_deliverylist, parent, false));
                break;
            case 3:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_productlist, parent, false));
                break;
            case 4:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_checkoutprom, parent, false));
                break;
            case 5:
                viewHolder = new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_checkoutaddup, parent, false));
                break;
        }

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {
        final PayCenterItem item = data.get(position);
//        holder.textView.setText(item.key);
        holder.expandableLayout.setInterpolator(item.interpolator);
        holder.expandableLayout.setExpanded(expandState.get(position));
        holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
            @Override
            public void onPreOpen() {
                createRotateAnimator(holder.buttonLayout, 0f, 180f).start();
                expandState.put(position, true);
            }

            @Override
            public void onPreClose() {
                createRotateAnimator(holder.buttonLayout, 180f, 0f).start();
                expandState.put(position, false);
            }
        });

        holder.buttonLayout.setRotation(expandState.get(position) ? 180f : 0f);
        holder.buttonLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                onClickButton(holder.expandableLayout);
            }
        });
    }

    private void onClickButton(final ExpandableLayout expandableLayout) {
        expandableLayout.toggle();
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView textView;
        public RelativeLayout buttonLayout;
        public ExpandableLinearLayout expandableLayout;

        public ViewHolder(View v) {
            super(v);
            textView = (TextView) v.findViewById(R.id.textView);
            buttonLayout = (RelativeLayout) v.findViewById(R.id.button);
            expandableLayout = (ExpandableLinearLayout) v.findViewById(R.id.expandableLayout);
        }
    }

    public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
        animator.setDuration(300);
        animator.setInterpolator(InterpolatorUtils.createInterpolator(InterpolatorUtils.LINEAR_INTERPOLATOR));
        return animator;
    }
}